package libx

import (
	"os"
	"path/filepath"
	"strings"
)

func WriteFile(path string, content string) error {
	file, err := os.Create(path)
	if err != nil {
		return err
	}
	defer file.Close()

	_, err = file.WriteString(content)
	if err != nil {
		return err
	}
	return nil
}

func ExtName(path string, toLower bool) string {
	if toLower {
		return strings.ToLower(filepath.Ext(path))
	} else {
		return filepath.Ext(path)
	}
}

func FileName(path string) string {
	baseName := filepath.Base(path)
	ext := filepath.Ext(baseName)
	return strings.TrimSuffix(baseName, ext)
}

func IsDir(path string) (bool, error) {
	fileInfo, err := os.Stat(path)
	if err != nil {
		return false, err
	}
	return fileInfo.IsDir(), nil
}
